package dao;

import common.Communication;
import common.Group;
import common.User;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;

public class ChatSend {
    //存发送的群聊消息
    public boolean groupChatsend(Communication u) {
        Connection conn=null;
        ResultSet res=null;
        try {
            Properties info=new Properties();
            info.load(new FileInputStream("src\\sql.properties"));
            String user=info.getProperty("user");
            String password=info.getProperty("password");
            String Driver=info.getProperty("Driver");
            String url=info.getProperty("url");
            Class.forName(Driver);
            conn= DriverManager.getConnection(url,user,password);
            String sql="insert into groupchat(sender,groupid,content,category,sendtime)values(?,?,?,?,?)";
            PreparedStatement presta = conn.prepareStatement(sql);
            presta.setString(1, u.getSender().getId());
            presta.setString(2, u.getGroup().getGroupid());
            presta.setString(3,u.getContent());
            presta.setInt(4,u.getType());
            presta.setString(5,u.getSendDateTime());
            int f=0;
            f=presta.executeUpdate();
            //System.out.println("send"+f);
            if(f!=0){
                return true;
            }else{
                return false;
            }
        }catch(SQLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }

    //群成员(转发群消息和显示群成员
    public ArrayList<Group> getGroupmember(Group user1) {
        ArrayList<Group> groupmembers= new ArrayList<Group>();
        Connection conn = null;
        ResultSet res = null;
        try {
            boolean f = false;
            Properties info=new Properties();
            info.load(new FileInputStream("src\\sql.properties"));
            String user=info.getProperty("user");
            String password=info.getProperty("password");
            String Driver=info.getProperty("Driver");
            String url=info.getProperty("url");
            //注册驱动
            Class.forName(Driver);
            //获取连接
            conn = DriverManager.getConnection(url, user, password);
            String sql = "select * from grouplist where group_id = ? and statement = ?";
            PreparedStatement presta = conn.prepareStatement(sql);
            presta.setString(1, user1.getGroupid());
            presta.setInt(2,1);
            res = presta.executeQuery();
            while (res.next()) {
                Group group=new Group();
                group.setuserid(res.getString("member_id"));
                System.out.println(group);
                groupmembers.add(group);
            }
            presta.setString(1, user1.getGroupid());
            presta.setInt(2,2);
            res = presta.executeQuery();
            while (res.next()) {
                Group group=new Group();
                group.setuserid(res.getString("member_id"));
                System.out.println(group);
                groupmembers.add(group);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return groupmembers;
    }

    //存发送的私聊消息
    public boolean chatSend(Communication u) {
        Connection conn=null;
        ResultSet res=null;
        try {
            Properties info=new Properties();
            info.load(new FileInputStream("src\\sql.properties"));
            String user=info.getProperty("user");
            String password=info.getProperty("password");
            String Driver=info.getProperty("Driver");
            String url=info.getProperty("url");
            Class.forName(Driver);
            conn= DriverManager.getConnection(url,user,password);
            String sql="insert into chat(sender,receiver,content,category,sendtime)values(?,?,?,?,?)";
            PreparedStatement presta = conn.prepareStatement(sql);
            System.out.println(u.getSender().getId());
            presta.setString(1, u.getSender().getId());
            presta.setString(2, u.getReceiver().getId());
            presta.setString(3,u.getContent());
            presta.setInt(4,u.getType());
            presta.setString(5,u.getSendDateTime());
            int f=0;
            f=presta.executeUpdate();
            //System.out.println("send"+f);
            if(f!=0){
                return true;
            }else{
                return false;
            }
        }catch(SQLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }

    //查找所有群成员
    public ArrayList<User> getGroupmembers(String groupid) {
        ArrayList<User> groupmembers= new ArrayList<User>();
        Connection conn = null;
        ResultSet res = null;
        try {
            boolean f = false;
            Properties info=new Properties();
            info.load(new FileInputStream("src\\sql.properties"));
            String user=info.getProperty("user");
            String password=info.getProperty("password");
            String Driver=info.getProperty("Driver");
            String url=info.getProperty("url");
            //注册驱动
            Class.forName(Driver);
            //获取连接
            conn = DriverManager.getConnection(url, user, password);
            String sql = "select * from grouplist where group_id = ? and statement = ?";
            PreparedStatement presta = conn.prepareStatement(sql);
            presta.setString(1, groupid);
            presta.setInt(2,1);
            res = presta.executeQuery();
            User member = new User();
            while (res.next()) {
                member.setId(res.getString("member_id"));
                System.out.println(member);
                groupmembers.add(member);
            }
            presta.setString(1, groupid);
            presta.setInt(2,2);
            res = presta.executeQuery();
            while (res.next()) {
                member.setId(res.getString("member_id"));
                System.out.println(member);
                groupmembers.add(member);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return groupmembers;
    }

    //是否为群主
    public boolean checkGroupowner(Group gp) {
        Connection conn = null;
        ResultSet res = null;
        User owner = new User();
        try {
            boolean f = false;
            Properties info=new Properties();
            info.load(new FileInputStream("src\\sql.properties"));
            String user=info.getProperty("user");
            String password=info.getProperty("password");
            String Driver=info.getProperty("Driver");
            String url=info.getProperty("url");
            //注册驱动
            Class.forName(Driver);
            //获取连接
            conn = DriverManager.getConnection(url, user, password);
            String sql = "select * from grouplist where group_id = ? and statement = ?";
            PreparedStatement presta = conn.prepareStatement(sql);
            presta.setString(1, gp.getGroupid());
            presta.setInt(2,2);
            res = presta.executeQuery();
            //owner.setUserid(res.getString("memberid"));
            if (res.next()) {
                System.out.println(gp.getuserid());
                System.out.println(res.getString("member_id"));
                if(gp.getuserid().equals(res.getString("member_id"))){
                    return true;
                }else{
                    return false;
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }
}
